<template>
  <div class="form-card">
    <div class="form-title">
      <i class="fas fa-calendar-alt form-icon"></i>
      <h2>选择预约时间</h2>
    </div>

    <div class="form-description">
      <p>当前医生：<span class="highlight">{{ selectedDoctor.name || '未选择' }}</span></p>
      <p>请选择该医生的出诊时间：</p>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state">
      <i class="fas fa-spinner fa-spin"></i>
      <p>加载医生排班中...</p>
    </div>

    <div v-else>
      <!-- 日期选择（只显示有排班的日期） -->
      <div class="time-selection">
        <h3>选择日期</h3>
        <div class="dates-grid">
          <div
            class="date-card"
            v-for="date in availableDates"
            :key="date.date"
            :class="{selected: formData.appointmentDate === date.date, unavailable: !date.available}"
            @click="selectDate(date.date)"
            :disabled="!date.available"
          >
            <div class="date-day">{{ date.day }}</div>
            <div class="date-number">{{ date.date.split('-')[2] }}</div>
            <div class="date-month">{{ date.month }}月</div>
            <div class="date-tag" v-if="!date.available">约满</div>
          </div>
        </div>

        <!-- 时间段选择（仅当选择日期后显示） -->
        <div v-if="formData.appointmentDate" class="period-selection-container">
          <h3>选择时段</h3>
          <div class="period-selection">
            <div
              class="period-card"
              :class="{selected: formData.appointmentPeriod === 'morning', unavailable: !availablePeriods.morning}"
              @click="selectPeriod('morning')"
              :disabled="!availablePeriods.morning"
            >
              <div class="period-icon">
                <i class="fas fa-sun"></i>
              </div>
              <div class="period-info">
                <h4>上午</h4>
                <p class="period-desc">08:00-12:00</p>
              </div>
            </div>
            <div
              class="period-card"
              :class="{selected: formData.appointmentPeriod === 'afternoon', unavailable: !availablePeriods.afternoon}"
              @click="selectPeriod('afternoon')"
              :disabled="!availablePeriods.afternoon"
            >
              <div class="period-icon">
                <i class="fas fa-cloud-sun"></i>
              </div>
              <div class="period-info">
                <h4>下午</h4>
                <p class="period-desc">14:00-18:00</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="form-buttons">
      <button type="button" class="btn btn-secondary" @click="goToPreviousStep">
        <i class="fas fa-arrow-left"></i>
        上一步
      </button>
      <button type="button" class="btn btn-primary" :disabled="!formData.appointmentDate || !formData.appointmentPeriod" @click="goToNextStep">
        下一步
        <i class="fas fa-arrow-right"></i>
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue'

const props = defineProps({
  modelValue: {
    type: Object,
    required: true
  },
  doctors: {
    type: Array,
    required: true
  },
  availableDates: {
    type: Array,
    required: true
  },
  availablePeriods: {
    type: Object,
    required: true
  },
  loading: {
    type: Boolean,
    required: true
  }
})

const emit = defineEmits(['update:modelValue', 'previous', 'next', 'loadSchedule', 'loadPeriods'])

const formData = ref({
  appointmentDate: props.modelValue.appointmentDate || null,
  appointmentPeriod: props.modelValue.appointmentPeriod || null
})

const selectedDoctor = computed(() => {
  return props.doctors.find(d => d.id === props.modelValue.doctorId) || {}
})

const selectDate = async (date) => {
  formData.value.appointmentDate = date
  formData.value.appointmentPeriod = null
  emit('loadPeriods', date)
}

const selectPeriod = (period) => {
  formData.value.appointmentPeriod = period
}

const goToPreviousStep = () => {
  emit('previous')
}

const goToNextStep = () => {
  if (!formData.value.appointmentDate || !formData.value.appointmentPeriod) {
    alert('请选择完整的预约时间！')
    return
  }
  
  // Emit updated form data to parent
  emit('update:modelValue', { 
    ...props.modelValue, 
    appointmentDate: formData.value.appointmentDate,
    appointmentPeriod: formData.value.appointmentPeriod
  })
  
  emit('next')
}

// Get weekday name
const getWeekDay = (dateString) => {
  const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return days[new Date(dateString).getDay()]
}

// Load schedule when doctor changes
watch(
  () => props.modelValue.doctorId,
  (newDoctorId) => {
    if (!newDoctorId) return
    emit('loadSchedule', newDoctorId)
  },
  { immediate: true }
)
</script>

<style scoped>
.form-card {
  background-color: white;
  border-radius: 16px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  padding: 30px;
  margin-bottom: 30px;
}

.form-title {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  border-bottom: 2px solid #f0f0f0;
  padding-bottom: 20px;
}

.form-icon {
  font-size: 32px;
  color: #3498db;
  margin-right: 15px;
}

.form-title h2 {
  font-size: 28px;
  font-weight: 700;
  color: #2980b9;
  margin: 0;
}

.form-description {
  margin-bottom: 25px;
}

.form-description p {
  font-size: 20px;
  color: #555;
  margin: 0;
}

.highlight {
  color: #3498db;
  font-weight: bold;
}

.time-selection {
  margin-bottom: 20px;
}

.time-selection h3 {
  font-size: 20px;
  margin-bottom: 15px;
  color: #333;
}

.dates-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 10px;
  margin-bottom: 30px;
}

.date-card {
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  padding: 15px 10px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  background: white;
}

.date-card:hover {
  border-color: #5dade2;
  transform: translateY(-2px);
}

.date-card.selected {
  border-color: #3498db;
  background-color: rgba(52, 152, 219, 0.1);
}

.date-day {
  font-size: 14px;
  color: #666;
  margin-bottom: 5px;
}

.date-number {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 5px;
}

.date-month {
  font-size: 14px;
  color: #666;
}

.period-selection-container {
  margin-top: 20px;
}

.period-selection {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.period-card {
  display: flex;
  align-items: center;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s;
  background: white;
}

.period-card:hover {
  border-color: #5dade2;
  transform: translateY(-2px);
}

.period-card.selected {
  border-color: #3498db;
  background-color: rgba(52, 152, 219, 0.1);
}

.period-card.unavailable {
  opacity: 0.5;
  cursor: not-allowed;
}

.period-icon {
  font-size: 32px;
  color: #3498db;
  margin-right: 15px;
}

.period-info h4 {
  font-size: 20px;
  margin: 0 0 5px 0;
  color: #333;
}

.period-info p {
  font-size: 16px;
  color: #666;
  margin: 0;
}

.period-desc {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

.date-card.unavailable {
  opacity: 0.5;
  cursor: not-allowed;
}

.date-tag {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background: #e74c3c;
  color: white;
  font-size: 10px;
  padding: 2px 4px;
  border-radius: 2px;
}

.loading-state {
  text-align: center;
  padding: 40px 0;
  color: #666;
}

.loading-state i {
  font-size: 40px;
  margin-bottom: 16px;
  color: #3498db;
}

.form-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 40px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 30px;
  font-size: 20px;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s;
  min-width: 160px;
}

.btn i {
  margin: 0 8px;
}

.btn-primary {
  background-color: #3498db;
  color: white;
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.btn-primary:disabled {
  background-color: #bdc3c7 !important;
  border-color: #bdc3c7 !important;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-secondary {
  background-color: #e0e0e0;
  color: #555;
}

.btn-secondary:hover {
  background-color: #d0d0d0;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.btn-secondary:disabled {
  background-color: #f0f0f0;
  color: #aaa;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .form-card {
    padding: 20px;
  }

  .form-title h2 {
    font-size: 24px;
  }

  .form-icon {
    font-size: 28px;
  }

  .form-description p {
    font-size: 18px;
  }

  .dates-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  .period-selection {
    grid-template-columns: 1fr;
  }

  .btn {
    padding: 14px 20px;
    font-size: 18px;
    min-width: 120px;
  }

  .form-buttons {
    flex-direction: column;
    gap: 15px;
  }

  .form-buttons .btn {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .dates-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>